<?php
session_start();
header('Content-type:text/html;charset=utf-8');
header('Content-type: application/json');
header('Access-Control-Allow-Origin:*');
include_once './dbConfig.php';
include_once './public.php';

if ($_GET) {
    if (isset($_GET['getType']) && $_GET['getType'] == 'search') {
        $search = $_GET['searchText'];
        $selectSql = 'select * from lgz_product where status =1 and title like "%' . $search . '%"';
        $datas = selectEvent($selectSql);
        $returnDatas = json_encode(reponseEvent($datas));
    }

    if (isset($_GET['getType']) && $_GET['getType'] == 'default') {
        $selectSql = 'select * from lgz_product where status=1 limit 0,' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }
    if (isset($_GET['getType']) && $_GET['getType'] == 'orderBySales') {
        $selectSql = 'SELECT * FROM `lgz_product`  where status =1  ORDER BY `sales`  ASC limit 0,' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }
    if (isset($_GET['getType']) && $_GET['getType'] == 'orderBySale') {
        $selectSql = 'SELECT *  FROM  `lgz_product`  where status =1   ORDER BY  `sales` DESC  LIMIT 0' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }
    if (isset($_GET['getType']) && $_GET['getType'] == 'Comprehensive') {
        $selectSql = 'SELECT * FROM `lgz_product`  where status =1  ORDER BY `type` DESC limit 0,' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }

    if (isset($_GET['getType']) && $_GET['getType'] == 'PriceSorting') {
        $selectSql = 'SELECT * FROM `lgz_product`  where status =1  ORDER BY `price` DESC limit 0,' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }

    if (isset($_GET['getType']) && $_GET['getType'] === 'max') {
        $max = $_GET['max'];
        $min = $_GET['min'];
        $selectSql = 'select * from lgz_product WHERE  status=1 and  price<' . $max . ' and price > ' . $min . ' order by price  DESC limit 0,' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }

    if (isset($_GET['getType']) && $_GET['getType'] == 'styleBySales') {
        $selectSql = 'SELECT *  FROM  `lgz_product`  where status =1   ORDER BY  `style` DESC limit 0' . $_GET['num'];
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }
    if (isset($_GET['getType']) && $_GET['getType'] == 'search') {
        $search = $_GET['search'];
        $selectSql = 'select * from lgz_product where status =1 and title like "%' . $search . '%"';
        $datas = selectEvent($selectSql);
        reponseEvent($datas);
    }
}

if ($_POST) {
    if (isset($_POST['postType']) && $_POST['postType'] == 'addOne') {
        $id = $_POST['id'];
        $num = 1;
        $username = $_POST['user'];
        $add_time = time() * 1000;

        $selectSql = 'select id,num from lgz_shop_car where status=1 and  pro_id=' . $id . ' and username="' . $username . '"';
        $datas = selectEvent($selectSql);
        if (count($datas) > 0) {
            $num = $datas[0]['num'] + 1;
            $updateSql = 'update lgz_shop_car set `num`=' . $num . ',`status`=1, `checked`=1 where id =' . $datas[0]['id'];
            if ($link->query($updateSql)) {
                $msg = ['msg' => '添加成功！', 'status' => 1];
            } else {
                $msg = ['msg' => '添加失败！', 'status' => 0];
            }
            reponseEvent($msg);
        }
        $insertSql = 'insert into lgz_shop_car (pro_id, num, username, add_time) VALUES ('
            . $id . ',' . $num . ',"' . $username . '","' . $add_time . '")';

        if ($link->query($insertSql)) {
            $msg = ['msg' => '添加成功！', 'status' => 1];
        } else {
            $msg = ['msg' => '添加失败！', 'status' => 0];
        }
        reponseEvent($msg);
    }
}